package com.taikang.hmp.hbusiness.doctor.selfdiagnosis.action.intface;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.datastructre.support.ResponseDto;
import com.taikang.hmp.frame.common.web.action.BaseAction;
import com.taikang.hmp.hbusiness.doctor.register.bean.Activation;
import com.taikang.hmp.hbusiness.doctor.selfdiagnosis.bean.HealthTag;
import com.taikang.hmp.hbusiness.doctor.selfdiagnosis.service.SelfDiagnosisService;

public class SelfDiagnosisAction extends BaseAction {
	
	private static Logger logger = LoggerFactory.getLogger(SelfDiagnosisAction.class);
	private SelfDiagnosisService selfDiagnosisService;
	
	public SelfDiagnosisService getSelfDiagnosisService() {
		return selfDiagnosisService;
	}

	public void setSelfDiagnosisService(SelfDiagnosisService selfDiagnosisService) {
		this.selfDiagnosisService = selfDiagnosisService;
	}

	@Override
	public String executeAction() {
		if ("getSymptomList".equals(requestDto.getMethod())) {
			logger.info("--获取症状列表: getSymptomList method --");
			return getSymptomList();
		}else if("getAutognosisQuestion".equals(requestDto.getMethod())){
			logger.info("--获取自诊问题: getAutognosisQuestion method --");
			return getAutognosisQuestion();
		}else {
			throw new TKException("no support method:" + requestDto.getMethod());
		}
	}
	//获取症状列表
	private String getSymptomList() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		List<Dto> resultList = selfDiagnosisService.getSymptomList(paramDto);
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		//
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	
	private String getAutognosisQuestion() {
		ResponseDto responseDto=new ResponseDto();
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		List<Dto> resultList = selfDiagnosisService.getSlefDiagnosis(paramDto);
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	
	/**
	 * 导入问卷问题数据
	 * @return
	 * @throws Exception
	 */
	public String getSymptomList1(){
		String fileName="C:\\aaa.xls";
		File importFile=new File(fileName);
		try {
			InputStream ins = new FileInputStream(importFile);//为导入文件准备的输入流
			HSSFWorkbook importBook = new HSSFWorkbook(ins);//创建工作薄
			HSSFSheet bookSheet = importBook.getSheetAt(0);//sheet页
			HSSFRow sheetRow=null;//行对象
			int sheetRows=bookSheet.getLastRowNum();
			for(int i=1;i<=63;i++){
				HealthTag he = new HealthTag();
				sheetRow=bookSheet.getRow(i);
				he.setHealthtagid(getCellValue(sheetRow.getCell(0)));
				he.setQuestion(getCellValue(sheetRow.getCell(1))==null?"":getCellValue(sheetRow.getCell(1)));
				he.setOptions(getCellValue(sheetRow.getCell(2))==null?"":getCellValue(sheetRow.getCell(2)));
				he.setHealthtag(getCellValue(sheetRow.getCell(3))==null?"":getCellValue(sheetRow.getCell(3)));
				he.setHealthresult(getCellValue(sheetRow.getCell(4))==null?"":getCellValue(sheetRow.getCell(4)));
				
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 导入问卷问题数据
	 * @return
	 * @throws Exception
	 */
	public String insertActivation(){
		String fileName="C:\\activation.xls";
		File importFile=new File(fileName);
		try {
			InputStream ins = new FileInputStream(importFile);//为导入文件准备的输入流
			HSSFWorkbook importBook = new HSSFWorkbook(ins);//创建工作薄
			HSSFSheet bookSheet = importBook.getSheetAt(0);//sheet页
			HSSFRow sheetRow=null;//行对象
			int sheetRows=bookSheet.getLastRowNum();
			for(int i=1;i<=200;i++){
				Activation qi = new Activation();
				sheetRow=bookSheet.getRow(i);
				qi.setCode_id(getCellValue(sheetRow.getCell(0)));
				qi.setActivation_code(getCellValue(sheetRow.getCell(1))==null?"":getCellValue(sheetRow.getCell(1)));
				qi.setIs_uesd(getCellValue(sheetRow.getCell(2))==null?"":getCellValue(sheetRow.getCell(2)));
//				qi.setDatacontent(getCellValue(sheetRow.getCell(3))==null?"":getCellValue(sheetRow.getCell(3)));
//				qi.setMaleis(getCellValue(sheetRow.getCell(4))==null?"":getCellValue(sheetRow.getCell(4)));
//				qi.setMaleno(getCellValue(sheetRow.getCell(5))==null?"":getCellValue(sheetRow.getCell(5)));
//				qi.setDepartment(getCellValue(sheetRow.getCell(6))==null?"":getCellValue(sheetRow.getCell(6)));
//				qi.setFemaleis(getCellValue(sheetRow.getCell(7))==null?"":getCellValue(sheetRow.getCell(7)));
//				qi.setFemaleno(getCellValue(sheetRow.getCell(8))==null?"":getCellValue(sheetRow.getCell(8)));

//				qi.setRoot(getCellValue(sheetRow.getCell(9))==null?"":getCellValue(sheetRow.getCell(9)));
				selfDiagnosisService.addQuestion(qi);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	
	/**
	 * 获取cell值
	 * @param cell
	 * @return
	 */
	private String getCellValue(Cell cell) {
		if (cell == null) {
			return null;
		} else {
			if (cell.getCellType() == 1) {
				return cell.getStringCellValue();
			} else if (cell.getCellType() == 0) {
				// transform double value into integer
				double value = cell.getNumericCellValue();
				int i = (int) (value);
				return i + "";
			} else {
				return null;
			}
		}
	}

}
